package handler

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"choujiang/box_server/service"
	"choujiang/common/consts"
	"choujiang/common/models"
	"choujiang/common/utils"
	"strconv"
)

type BannerString struct {
	Url string `json:"url"`
}

/**
 获取分享使用的token
 */
func GetShareToken(c *gin.Context) {
	form := c.Request.Form
	goodsId := form.Get("goodsId")
	uniqueId := form.Get("uniqueId")
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	int64Id, err := strconv.ParseInt(goodsId, 10, 64)
	//获取shareToken
	shareToken, err := service.GetUserShareToken(user, int64Id)
	if err != nil {
		fmt.Printf("GetShareToken err:%+v\n", err)
	}

	utils.JSON(shareToken, c)
}

/**
 分享到群领取奖励
 */
func ShareGroup(c *gin.Context) {
	form := c.Request.Form
	iv := form.Get("iv")
	encryptedData := form.Get("encryptedData")
	token := form.Get("shareToken")
	gameId := form.Get("gameId")
	openId := form.Get("openId")
	if token == "" {
		utils.Error(consts.ErrInvalidParams, c)
		return
	}
	reInt, shareStruct := service.ValidateShareToken(token)
	if reInt == 0 {
		utils.Error(consts.ErrInvalidShareToken, c)
		return
	}
	if iv != "" && encryptedData != "" {
		errType := service.GetShareReward(iv, encryptedData, shareStruct, gameId, openId)
		if errType != nil {
			utils.Error(errType, c)
			return
		}
	}

	utils.JSON("ok", c)
}

/**
 获取分享二维码地址
 */
func GetQrscene(c *gin.Context) {
	form := c.Request.Form
	uniqueId := form.Get("uniqueId")
	gameId := form.Get("gameId")
	goodsId := form.Get("goodsId")
	page := form.Get("page")
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	goodsId64, _ := strconv.ParseInt(goodsId, 10, 64)
	poster := model.GetPosterById(1)
	if poster == nil {
		utils.Error(consts.ErrNotFoundPoster, c)
		return
	}
	goods := model.GetGoodsById(goodsId64)
	if goods == nil {
		utils.Error(consts.ErrGoodsNotFound, c)
		return
	}
	var banners []BannerString
	var cover string
	err := json.Unmarshal([]byte(goods.Banners), &banners)
	if err != nil {
		fmt.Printf("-------------------err: %s", err)
	}
	cover = utils.AddPrefix(banners[0].Url)
	qrscene, errType := service.GetUserQrscene(user, gameId, poster, page, goods.Id, cover)
	if errType != nil {
		utils.Error(errType, c)
		return
	}
	utils.JSON(qrscene, c)
}

/**
 获取分享页面内容
 */
func GetSharePage(c *gin.Context) {
	form := c.Request.Form
	uniqueId := form.Get("uniqueId")
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	sharePage, err := service.GetSharePage()
	if err != nil {
		fmt.Printf("GetSharePage err:%+v\n", err)
	}
	utils.JSON(sharePage, c)
}

/**
 @description 获取SDK跳转token
 @return mixed
 @author guxiang
 @version v1 2018-11-23
 */
func GetSdkToken(c *gin.Context) {
	form := c.Request.Form
	uniqueId := form.Get("uniqueId")
	toGame := form.Get("toGame")
	shareToken := form.Get("shareToken")
	user := model.GetUserByUniqueId(uniqueId)
	if user == nil {
		utils.Error(consts.ErrUserNotFound, c)
		return
	}
	//sdkToken
	sdkToken, err := service.GetJumpSdkToken(user, toGame, shareToken)
	if err != nil {
		fmt.Printf("GetJumpSdkToken err:%+v\n", err)
	}
	utils.JSON(sdkToken, c)
}
